<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"/>
  <title>增减分页 - JRoll.Infinite</title>
  <script src="../../download/jroll.min.js"></script>
  <script src="../../download/jroll.infinite.min.js"></script>
  <style>
  html,body {width:100%;height:100%;margin:0;padding:0;overflow:hidden;}
  #wrap {width:100%;height:100%;overflow:hidden;}
  .jroll-infinite-tip {height:32px;line-height:32px;text-align:center;}
  .item {
    line-height: 32px;
  }
  .item img {
    width:100%;
    display:block;
  }
  .item p {
    text-align: center;
    padding: 0;
    margin: 0;
    background: #eee;
  }
  </style>
</head>
<body>
  <div id="wrap">
    <div id="scroller"></div>
  </div>
  <script>
  //模拟ajax
  function ajax(parms) {
    var a = [], i;
    var page = parseInt(parms.url.replace("getData.do?page=", ""), 10);
    for (i=1; i<=3; i++) {
      a.push({
        "index" : i+(page-1)*3,
        "img" : page % 2 === 0 ? "../../images/"+(i+3)+".jpg" : "../../images/"+i+".jpg"
      });
    }
    setTimeout(function() {
      parms.success(a);
    }, 1000);
  }
  
  var infinite = new JRoll_Infinite("#wrap", {
    total: 9, //总页数
    limit: true,
    getData: function(page, callback) {   //获取数据的函数，必须传递一个数组给callback
      ajax({
        url : "getData.do?page="+page,
        success: function(data) {
          callback(data);
          
          var imgs = document.querySelectorAll(".item img");
          for (var i=0, l=imgs.length; i<l; i++) {
            imgs[i].onload = function() {
              infinite.jroll.refresh();
            }
          }
        }
      });
    },
    template: "<div class='item'><img src='{{=_obj.img}}' /><p>{{=_obj.index}}</p></div>" //每条数据模板
  });
  </script>
</body>
</html>